home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / kiss-0.11 / kiss-0 / kiss / src / lexer < prev    next >
Text File  |  1995-04-24  |  5KB  |  236 lines

  1. /* normal C section */
  2. %{
  3.  
  4. #include "kiss.h"
  5. #include "parser.tab.h"
  6.  
  7. #ifdef USE_READLINE            /* support for GNU's readline */
  8.  
  9. static int ignorechar (int count, int key)
  10. {
  11.     /* reset buffer */
  12.     *rl_line_buffer = '\0';
  13.  
  14.     return (0);
  15. }    
  16.     
  17. static int myread (char *buf, int max)
  18. {
  19.     static char
  20.     *lineread;
  21.     register int
  22.     res;
  23.     static int
  24.     initialized;
  25.  
  26.     /* see if we need more keybindings */
  27.     if (! initialized)
  28.     {
  29.     initialized++;
  30.     rl_bind_key (3, ignorechar);
  31.     }
  32.  
  33.     /* if not interactive mode: just do the read */
  34.     if (! isatty (fileno (yyin)) )
  35.     {
  36.     if ( (res = read (fileno (yyin), buf, max)) < 0 )
  37.         res = 0;
  38.         return (res);
  39.     }
  40.     
  41.  
  42.     /* free previous line if necessary */
  43.     if (lineread)
  44.     {
  45.     free (lineread);
  46.     lineread = NULL;
  47.     }
  48.  
  49.     /* get the input */
  50.     lineread = readline (getprompt ());
  51.  
  52.     /* EOF condition? */
  53.     if (! lineread)
  54.     {
  55.     *buf = '\0';
  56.     return (0);
  57.     }
  58.     /* \n character? */
  59.     if (! *lineread)
  60.     {
  61.     *buf = '\n';
  62.     return (1);
  63.     }
  64.  
  65.     /* otherwise: must be a string */
  66.     if (strlen (lineread) > max - 1)
  67.     {
  68.     warning ("[lexer] input buffer overflow");
  69.     *buf = '\n';
  70.     return (1);
  71.     }
  72.     strcpy (buf, lineread);
  73.     strcat (buf, "\n");
  74.     add_history (lineread);
  75.     return (strlen (buf));
  76. }    
  77.  
  78. #endif                    /* USE_READLINE */
  79.  
  80. #ifdef USE_GETLINE            /* getline support */
  81. static int myread (char *buf, int max)
  82. {
  83.     register char
  84.     *input;
  85.     register int
  86.     res;
  87.  
  88.     if (! isatty (fileno (yyin)))   /* do read() if in a pipe or so */
  89.     {
  90.     if ( (res = read (fileno (yyin), buf, max)) < 0 )
  91.         res = 0;
  92.     return (res);
  93.     }
  94.  
  95.     input = getline (getprompt ()); /* otherwise: the getline version */
  96.  
  97.     if (! *input)            /* EOF */
  98.     return (0);
  99.  
  100.     if (strlen (input) > max - 1)   /* line too long ? */
  101.     {
  102.     warning ("[lexer] input buffer overflow");
  103.     *buf = '\n';
  104.     return (1);
  105.     }
  106.     
  107.     strcpy (buf, input);
  108.     gl_histadd (input);
  109.     return (strlen (input));
  110. }
  111.     
  112.     
  113.  
  114. #endif                    /* USE_GETLINE */
  115.  
  116. #ifdef USE_BARE                /* no getline, no readline */
  117.  
  118. static int myread (char *buf, int max)
  119. {
  120.     int
  121.     res;
  122.     
  123.     /* show prompt if interactive */
  124.     if (isatty (fileno (yyin)))
  125.     {
  126.     fflush (stdout);
  127.     fflush (stderr);
  128.     printf ("%s", getprompt ());
  129.     fflush (stdout);
  130.     } 
  131.  
  132.     /* do the read */
  133.     if ( (res = read (fileno (yyin), buf, max)) < 0 )
  134.     res = 0;
  135.     return (res);
  136. }    
  137.  
  138. #endif                    /* USE_BARE */
  139.  
  140.  
  141.  
  142. /* remap YY_INPUT to one of the versions of myread () */
  143. #undef YY_INPUT
  144. #define YY_INPUT(buf,res,max)   res = myread (buf, max)
  145.  
  146. /* remap the macro yywrap to indicate that more files are there */
  147. #undef yywrap
  148. int yywrap ()
  149. {
  150.     /* if closing sourced file: continue parsing next buffer */
  151.     if (yypopfile ())
  152.     return (0);
  153.     
  154.     /* if no scripts (interactive mode): we're done */
  155.     if (! lastfile)
  156.     return (1);
  157.  
  158.     while (1)
  159.     {
  160.     /* done with this script */
  161.     if (yyin)
  162.     {
  163.         fclose (yyin);
  164.         yyin = NULL;
  165.     }
  166.     lastfile++;
  167.  
  168.     /* if no more scripts: we're done */
  169.     if (lastfile >= orgargc)
  170.         return (1);
  171.  
  172.     /* if we can open it, then proceed */
  173.     if ( (yyin = fopen (orgargv [lastfile], "r")) )
  174.         return (0);
  175.  
  176.     /* issue warning and continue with next file */
  177.     warning ("cannot open script \"%s\" for reading", orgargv [lastfile]);
  178.     }
  179. }    
  180.  
  181. %}
  182.  
  183. /* rules */
  184. %%
  185. \!                { return (RECALLCMD); }
  186. \|                { return (PIPETO); }
  187. \&                { return (BACKGROUND); }
  188. \>                { return (REDIRTO); }
  189. \>\>                { return (REDIRAPPEND); }
  190. \<                { return (REDIRFROM); }
  191. \$[a-zA-Z]+            { return (VARIABLE); }
  192. \$\$                { return (VARIABLE); }
  193. \$\?                { return (VARIABLE); }
  194. \$\!                { return (VARIABLE); }
  195. \'.*\'                { return (SQUOTESTRING); }
  196. \".*\"                { return (DQUOTESTRING); }
  197. \`.*\`                { return (BQUOTESTRING); }
  198. \#.+                /* ignore */ ;
  199. [^\ \!\;\n\t\|\&\>\<\=]+    { return (IDENT); }
  200. \=                { return (IDENT); }
  201. \n                { return (ENDLINE); }
  202. \;                { return (ENDLINE); }
  203. [\ \t]+                /* ignore */ ;
  204. \\[ \t]*\n            /* ignore */ ;
  205. .                {
  206.                     warning ("[lexer] unmatched "
  207.                          "input \"%s\"",
  208.                          yytext);
  209.                 }
  210.  
  211. %%
  212. /* support for pushing and popping files, used in "source" */
  213. static YY_BUFFER_STATE
  214.     *bufstate;
  215. static int
  216.     nbufstate;
  217.     
  218. void yypushfile (FILE *inf)
  219. {
  220.     bufstate = realloc (bufstate, (nbufstate + 1) * sizeof (YY_BUFFER_STATE));
  221.     bufstate [nbufstate] = yy_create_buffer (inf, YY_BUF_SIZE);
  222.     yy_switch_to_buffer (bufstate [nbufstate++]);
  223. }
  224.  
  225. int yypopfile ()
  226. {
  227.     if (nbufstate > 1)
  228.     {
  229.     nbufstate--;
  230.     yy_delete_buffer (bufstate [nbufstate]);
  231.     yy_switch_to_buffer (bufstate [nbufstate - 1]);
  232.     return (1);
  233.     }
  234.     return (0);
  235. }
  236.